package com.tanhua.server.impl;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.dubbo.api.PeachblossomApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.exception.BusinessException;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.domain.ErrorResult;
import com.tanhua.model.mongo.Sound;
import com.tanhua.model.vo.SoundVo;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
public class PeachblossomService {
    @DubboReference
    private PeachblossomApi peachblossomApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;

    public void savePeachblossom(MultipartFile soundFile) throws IOException {
        if (soundFile.isEmpty()) {
            throw new BusinessException(ErrorResult.error());
        }
        String filename = soundFile.getOriginalFilename();
        filename = filename.substring(filename.lastIndexOf(".") + 1);
        StorePath storePath = fastFileStorageClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), filename, null);
        String soundUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
        Sound sound = new Sound();
        sound.setSoundUrl(soundUrl);
        sound.setUserId(UserHolder.getUserId());
        sound.setCreated(System.currentTimeMillis());
        peachblossomApi.savePeachbossom(sound);
    }

    public SoundVo getPeachblossom() {
        Sound sound = peachblossomApi.getPeachblossom();
        UserInfo userInfo = userInfoApi.findById(sound.getUserId());
        SoundVo soundVo = new SoundVo();
        if (userInfo != null) {
            BeanUtils.copyProperties(userInfo, soundVo);
        }
        soundVo.setSoundidUrl(sound.getSoundUrl());
        Long userId = UserHolder.getUserId();
        //设置key
        String key = "SOUND" + userId;
        Boolean aBoolean = redisTemplate.hasKey(key);
        //设置剩余次数
        if (aBoolean) {
            Integer count = (Integer) redisTemplate.opsForValue().get(key);
            soundVo.setRemainingTimes(--count);
            if(count<1){
                throw new BusinessException(ErrorResult.error());
            }
            redisTemplate.opsForValue().set(key, count);
            return soundVo;
        }
            Integer count = 7;
            redisTemplate.opsForValue().set(key, count,24, TimeUnit.MINUTES);
            soundVo.setRemainingTimes(count);
            return soundVo;
    }
}
